
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;

public class HeadObj extends GameObj {
    private String direction="right" ;

    public String getDirection() { return direction; }
    public void setDirection(String direction) { this.direction = direction; }

    public HeadObj(Image img, int x, int y, GameWin frame) {
        super(img, x, y, frame);
     //监听键盘
        this.frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) { changeDirection(e); }
        });
    }

    // 方向控制
    public void changeDirection(KeyEvent e){
        switch (e.getKeyCode()){
            case KeyEvent.VK_A: if(!"right".equals(direction)){ direction="left"; img= GameUtils.leftImg; } break;
            case KeyEvent.VK_D: if(!"left".equals(direction)){ direction="right"; img= GameUtils.rightImg; } break;
            case KeyEvent.VK_W: if(!"down".equals(direction)){ direction="up"; img= GameUtils.upImg; } break;
            case KeyEvent.VK_S: if(!"up".equals(direction)){ direction="down"; img= GameUtils.downImg; } break;
            default: break;
        }
    }

    // 蛇移动逻辑
    public void move(){
        List<BodyObj> bodyObjList=this.frame.bodyObjsList;
        for(int i=bodyObjList.size()-1;i>=1;i--){
            bodyObjList.get(i).x=bodyObjList.get(i-1).x;
            bodyObjList.get(i).y=bodyObjList.get(i-1).y;
            if(this.x==bodyObjList.get(i).x&&this.y==bodyObjList.get(i).y){
                GameWin.state=3; // 碰撞身体游戏失败
            }
        }
        bodyObjList.get(0).x=this.x;
        bodyObjList.get(0).y=this.y;
        switch (direction){
            case "up": y-=height; break;
            case "down": y+=height; break;
            case "left": x-=width; break;
            case "right": x+=width; break;
            default: break;
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        Integer newX=null;
        Integer newY=null;
        FoodObj foodObj = new FoodObj();
        // 遍历食物列表，判断是否吃到食物
        Iterator<FoodObj> iterator = frame.foodObjList.iterator();
        while (iterator.hasNext()) {
            FoodObj food = iterator.next();
            if (this.x == food.x && this.y == food.y) {
                // 吃到食物：移除当前食物并补充新食物
                iterator.remove();
                frame.foodObjList.add(foodObj.getFood());
                // 获取蛇身最后一节坐标，用于新增身体
                BodyObj lastBody = frame.bodyObjsList.get(frame.bodyObjsList.size() - 1);
                newX = lastBody.x;
                newY = lastBody.y;
                frame.score++; // 分数+1
                break;
            }
        }

        // 规则：1-3关分数≥15通关，第四关分数≥30通关，第五关无尽模式，无通关
        if(!GameUtils.isEndlessMode){
            if(GameUtils.level ==4 && frame.score>=5){
                GameWin.state=4; // 第四关通关
            }else if(GameUtils.level !=4 && frame.score>=3){
                GameWin.state=4; // 1-3关通关
            }
        }

        // 移动蛇
        move();
        // 新增蛇身
        if(newX!=null&&newY!=null){
            frame.bodyObjsList.add(new BodyObj(GameUtils.bodyImg,newX,newY,frame));
        }
        // 越界处理
        if(x<0){x=570;}else if(x>570){ x=0; } else if (y<30) { y=570; } else if (y>570) { y=30; }
    }
}